// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2024 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.13.1
package sys

import (
	"context"
	"fmt"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/convert"
	"hotgo/utility/excel"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

type sSysStockZhASpotEm struct{}

func NewSysStockZhASpotEm() *sSysStockZhASpotEm {
	return &sSysStockZhASpotEm{}
}

func init() {
	service.RegisterSysStockZhASpotEm(NewSysStockZhASpotEm())
}

// Model 每日股票数据-东财ORM模型
func (s *sSysStockZhASpotEm) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.StockZhASpotEm.Ctx(ctx), option...)
}

// List 获取每日股票数据-东财列表
func (s *sSysStockZhASpotEm) Statics(ctx context.Context) (list *sysin.StockZhASpotStatics, err error) {

	mod := s.Model(ctx)
	zfGt7, _ := mod.Clone().WhereGTE("change_percent", 7).Count()
	zfBt57, _ := mod.Clone().
		WhereGTE("change_percent", 5).
		WhereLT("change_percent", 7).
		WhereGT("last_price", 0).
		Count()
	zfBt25, _ := mod.Clone().
		WhereGTE("change_percent", 2).
		WhereLT("change_percent", 5).
		WhereGT("last_price", 0).
		Count()
	zfBt02, _ := mod.Clone().
		WhereGTE("change_percent", 0).
		WhereLT("change_percent", 2).
		WhereGT("last_price", 0).
		Count()
	zfBt0N2, _ := mod.Clone().
		WhereGTE("change_percent", -2).
		WhereLT("change_percent", 0).
		WhereGT("last_price", 0).
		Count()
	zfBtN2N5, _ := mod.Clone().
		WhereGTE("change_percent", -5).
		WhereLT("change_percent", -2).
		WhereGT("last_price", 0).
		Count()
	zfBtN5N7, _ := mod.Clone().
		WhereGTE("change_percent", -7).
		WhereLT("change_percent", -5).
		WhereGT("last_price", 0).
		Count()
	zfLt7, _ := mod.Clone().WhereLT("change_percent", -7).
		WhereGT("last_price", 0).
		Count()
	zfZero, _ := mod.Clone().Where("change_percent", 0).
		WhereGT("last_price", 0).
		Count()

	return &sysin.StockZhASpotStatics{
		ZfGt7:    zfGt7,
		ZfBt57:   zfBt57,
		ZfBt25:   zfBt25,
		ZfBt02:   zfBt02,
		ZfBt0N2:  zfBt0N2,
		ZfBtN2N5: zfBtN2N5,
		ZfBtN5N7: zfBtN5N7,
		ZfLt7:    zfLt7,
		ZfZero:   zfZero,
	}, nil
}

// List 获取每日股票数据-东财列表
func (s *sSysStockZhASpotEm) List(ctx context.Context, in *sysin.StockZhASpotEmListInp) (list []*sysin.StockZhASpotEmListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 查询代码
	if in.Code != "" {
		mod = mod.WhereLike(dao.StockZhASpotEm.Columns().Code, in.Code)
	}

	if len(in.Codes) != 0 {
		mod = mod.WhereIn(dao.StockZhASpotEm.Columns().Code, in.Codes)
	}

	totalCount, err = mod.Clone().Count()
	if err != nil {
		err = gerror.Wrap(err, "获取每日股票数据-东财数据行失败，请稍后重试！")
		return
	}

	if totalCount == 0 {
		return
	}

	// 默认排序
	if len(in.Sorters.GetSorters()) == 0 {
		in.Sorters.Sorters = append(in.Sorters.Sorters, &form.Sorter{ColumnKey: "changePercent", Order: "descend"})
	}
	if err = mod.Fields(sysin.StockZhASpotEmListModel{}).Page(in.Page, in.PerPage).Handler(handler.Sorter(in)).Scan(&list); err != nil {
		err = gerror.Wrap(err, "获取每日股票数据-东财列表失败，请稍后重试！")
		return
	}
	return
}

// Export 导出每日股票数据-东财
func (s *sSysStockZhASpotEm) Export(ctx context.Context, in *sysin.StockZhASpotEmListInp) (err error) {
	list, totalCount, err := s.List(ctx, in)
	if err != nil {
		return
	}

	// 字段的排序是依据tags的字段顺序，如果你不想使用默认的排序方式，可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
	tags, err := convert.GetEntityDescTags(sysin.StockZhASpotEmExportModel{})
	if err != nil {
		return
	}

	var (
		fileName  = "导出每日股票数据-东财-" + gctx.CtxId(ctx) + ".xlsx"
		sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
		exports   []sysin.StockZhASpotEmExportModel
	)

	if err = gconv.Scan(list, &exports); err != nil {
		return
	}

	err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
	return
}

// Edit 修改/新增每日股票数据-东财
func (s *sSysStockZhASpotEm) Edit(ctx context.Context, in *sysin.StockZhASpotEmEditInp) (err error) {
	var res *sysin.StockZhASpotEmViewModel
	if err = s.Model(ctx).WherePri(in.Code).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取每日股票数据-东财信息，请稍后重试！")
		return
	} else {
		if res == nil {
			// 新增
			if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
				Fields(sysin.StockZhASpotEmInsertFields{}).
				Data(in).Insert(); err != nil {
				err = gerror.Wrap(err, "新增每日股票数据-东财失败，请稍后重试！")
			}
		} else {
			// 修改
			if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
				Fields(sysin.StockZhASpotEmUpdateFields{}).
				Data(in).
				WherePri(in.Code).
				Update(); err != nil {
				err = gerror.Wrap(err, "修改每日股票数据-东财失败，请稍后重试！")
			}
		}
	}

	return
}

// Delete 删除每日股票数据-东财
func (s *sSysStockZhASpotEm) Delete(ctx context.Context, in *sysin.StockZhASpotEmDeleteInp) (err error) {
	if _, err = s.Model(ctx).WherePri(in.Code).Delete(); err != nil {
		err = gerror.Wrap(err, "删除每日股票数据-东财失败，请稍后重试！")
		return
	}
	return
}

// View 获取每日股票数据-东财指定信息
func (s *sSysStockZhASpotEm) View(ctx context.Context, in *sysin.StockZhASpotEmViewInp) (res *sysin.StockZhASpotEmViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Code).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取每日股票数据-东财信息，请稍后重试！")
		return
	}
	return
}
